package com.xiaoxiong.mami.http;

import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;


/**
 * 
 * encode
 * 
 * @author zcg<br/>
 * @version 1.0<br/>
 * @email: zcg@suntektech.com<br/>
 * @datetime: 2012-12-18<br/>
 */
public class GZIPDecoder {

	private static Logger logger = Logger.getLogger(GZIPDecoder.class);

	/**
	 * 
	 * 
	 * @param in
	 * @param charset
	 * @return
	 */
	public static byte[] decode(InputStream in) {
		try {
			GZIPInputStream gin = new GZIPInputStream(in);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = gin.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			gin.close();

			return out.toByteArray();
		} catch (IOException e) {
			logger.error("decode inputstream exception: " + e, e);
		}
		return null;
	}

}
